// 这个文件专门存放关于社区的所有接口

const residential=require('koa-router')()
const db=require("../util/db")

// https://api.lzjlw.top:3456/residential/community
residential.get("/community",async (ctx)=>{
    let selectSql=`select dynamicId,user.userId,userName,head_Photo,content,public_Time,type 
    from community,user where community.userId=user.userId`
    let res=await new Promise((resolve,reject)=>{
        db.query(selectSql,(err,result)=>{
            if(err){
                throw err
            }else{
                resolve(result)
            }
        })
    })
    ctx.body=res
})


// https://api.lzjlw.top:3456/residential/community_Comments?dynamicId=${dynamicId}
residential.get("/community_Comments",async (ctx)=>{
    let obj=ctx.query
    console.log(obj)
    var dynamicId=obj.dynamicId
    let selectSql=`select dynamicId,commentId,userName,content,public_Time,type from community_Comments,user 
    where dynamicId=${dynamicId} and community_Comments.userId=user.userId`
    let res=await new Promise((resolve,reject)=>{
        db.query(selectSql,(err,result)=>{
            if(err){
                throw err
            }else{
                resolve(result)
            }
        })
    })
    ctx.body=res[0]
})


// https://api.lzjlw.top:3456/residential/community_Like?type=3 && from_Id=${dynamicId} && userId=${userId}
residential.get('/community_Like',async (ctx)=>{
    let obj=ctx.query
    // 利用正则表达式去除空格
    let str = JSON.stringify(obj).toString().replace(/\s*/g,"")
    newObj=JSON.parse(str)
    console.log(newObj.type)
    let selectSql
    // 通过type值判断点赞的内容是动态还是动态评论
    // 如果是点赞的是动态，则将动态表和点赞表连接
    if(newObj.type==3){
        selectSql=`select * from community_Like,community 
        where community_Like.type=${newObj.type} and from_Id=${newObj.from_Id} and 
        community_Like.userId=${newObj.userId} and community.dynamicId=community_Like.from_Id`
    }
    // 如果是点赞的是动态评论，则将动态评论表和点赞表连接
    else if(newObj.type==4){
        selectSql=`select * from community_Like,community_Comments 
        where community_Like.type=${newObj.type} and from_Id=${newObj.from_Id} and 
        community_Like.userId=${newObj.userId} and community_Comments.commentId=community_Like.from_Id`
    }
    let res=await new Promise((resolve,reject)=>{
            db.query(selectSql,(err,result)=>{
                if(err){
                    throw err
                }else{
                    if(result.length==0){
                        resolve(false)
                    }
                    else{
                        resolve(true)
                    }
                }
            })
        })
    
    ctx.body=res
})


// POST请求
// https://api.lzjlw.top:3030/residential/community_dynamic?type=3
residential.post('/community_dynamic',async (ctx)=>{
    const type=ctx.query.type
    let dynamicId=Math.floor((Math.random()*1000000))
    let commentId=Math.floor((Math.random()*1000000))
    let obj=ctx.request.body
    let time=new Date().getTime()
    if(type==3){
        let insertSql=`insert into community(dynamicId,userId,content,public_Time,type) 
        values(${dynamicId},${obj.userId},'${obj.content}',${time},${type})`
        await new Promise((resolve,reject)=>{
            db.query(insertSql,(err,result)=>{
                if(err){
                    console.log(err)
                }
                else{
                    resolve('insert success')
                }
            })
        })
        var dynamic={
            dynamicId:dynamicId,
            public_Time:time
        }
        ctx.body=dynamic
    }else if(type==4){
        let insertSql=`insert into community_Comments(commentId,userId,content,public_Time,type) 
        values(${commentId},${obj.userId},'${obj.content}',${time},${type})`
        await new Promise((resolve,reject)=>{
            db.query(insertSql,(err,result)=>{
                if(err){
                    console.log(err)
                }
                else{
                    resolve('insert success')
                }
            })
        })
        var comment={
            commentId:commentId,
            public_Time:time
        }
        ctx.body=comment
    }

})


// https://api.lzjlw.top:3030/residential/community_dynamic?type=3&dynamicId=
residential.delete('/community_dynamic',async (ctx)=>{
    let obj=ctx.query
    if(obj.type==3){
        let deleteSql=`delete from community where type=${obj.type} and dynamicId=${obj.dynamicId}`
        let res=await new Promise((resolve,reject)=>{
            db.query(deleteSql,(err,result)=>{
                if(err){
                    console.log(err)
                }
                else{
                    resolve('delete success')
                }
            })
        })
        ctx.body=res
    }
    else if(obj.type==4){
        let deleteSql=`delete from community_Comments where type=${obj.type} and commentId=${obj.commentId}`
        let res=await new Promise((resolve,reject)=>{
            db.query(deleteSql,(err,result)=>{
                if(err){
                    console.log(err)
                }
                else{
                    resolve('delete success')
                }
            })
        })
        ctx.body=res
    }
    
})

module.exports=residential